package com.dingshi.security.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingshi.security.R;
import com.dingshi.security.entity.Diary;

public class DiaryAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private Context context;
	private ArrayList<Diary> list;

	public DiaryAdapter(Context context, ArrayList<Diary> list) {
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Diary diary = list.get(position);
		if (diary == null) {
			return null;
		}
		final ViewHolder holder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.diary_item, null);
			holder = new ViewHolder();
//			holder.diaryItem = (LinearLayout) convertView.findViewById(R.id.diary_item);
			holder.content = (TextView)convertView.findViewById(R.id.content);
			holder.time = (TextView)convertView.findViewById(R.id.timestamp);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		updateData(holder, diary);

		return convertView;
	}

	private void updateData(ViewHolder holder, Diary diary) {
		holder.content.setText(diary.getContent());
		holder.time.setText(diary.getCreateTime());
		/*if(contentList!=null) {
				TextView text = null;
			holder.diaryItem.removeAllViews();
			for(int i=0;i<contentList.size();i++) {
				text = new TextView(context);
				text.setText(i+". "+contentList.get(i));
//				text.setTextSize(16.f);
//				text.setTextColor(context.getResources().getColor(R.color.black));
				holder.diaryItem.addView(text);
			}
			holder.time.setText(convertTime(diary.getTimeStamp()));
		}*/

	}
	private String convertTime(long createTime) {
		Date date = new Date(createTime);
		SimpleDateFormat format = new SimpleDateFormat("hh:MM");
		return format.format(date);
	}
	static class ViewHolder {
		LinearLayout diaryItem;
		TextView content;
		TextView time;
	}
}


